import { homeRouterMap, constantRouterMap } from 'src/router';

/**
 * 是否有权限访问路由
 * 
 * @param {array} roles 角色
 * @param {array} route 路由
 * @returns {boolean} 是否有权限
 */
// function hasPermission(roles, route) {
//     if (route.meta && route.meta.role) {
//         return roles.some(role => route.meta.role.indexOf(role) >= 0)
//     } else {
//         return true
//     }
// }
// 递归路由与数据
function filterRoute(routeList, filterList) {
    var ret = routeList.filter((item, i) => {
        let fItem = null;
        filterList.forEach(f => {
            if (f.path == item.path) {
                fItem = f;
            }
        });
        if (fItem) {
            if (item.children) {
                if (fItem.children) {
                    if (fItem.markedLvl > 1) {
                        filterRoute(item.children, fItem.children);
                    }
                } else {
                    delete item.children;
                }
            }
            return true;
        } else {
            return false;
        }
    });
    routeList.length = 0;
    ret.forEach(item => routeList.push(item));
}

function fixHomePath(routerMap) {
    let first = routerMap[1];
    let second = first.children[0].path;
    let redirect;
    if (second[0] === '/') redirect = second;
    else redirect = first.path + '/' + second;
    routerMap[0].redirect = redirect;
}

function fixRedirect(routerMap) { // 统一更新item的redirect为children第一项
    routerMap.forEach(item => {
        if (item.children) {
            if (item.redirect) {
                if (item.children.length == 0) return
                if (item.children[0].path[0] == '/') {
                    item.redirect = item.children[0].path;
                } else {
                    item.redirect = item.path + '/' + item.children[0].path;
                }
            }
            fixRedirect(item.children);
        } else {
            return
        }
    });
}

//展示路由层级

function markLvl(filterList, lvl) {
    filterList.forEach(m => {
        if (lvl > 0) {
            m.markedLvl = lvl;
            if (m.children) {
                markLvl(m.children, lvl - 1);
            }
        }
    });
}

const permission = {
    state: {
        routers: constantRouterMap,
        addRouters: []
    },

    mutations: {
        SET_ROUTERS: (state, routers) => {
            state.addRouters = routers;
            state.routers = constantRouterMap.concat(routers);
            // console.log(state.routers)
        }
    },

    actions: {
        GenerateRoutes({ commit }, data) {
            /* global Promise */
            return new Promise(resolve => {
                // const { roles } = data;
                const routeBase = location.pathname.substr(1);
                let routerMap;
                switch (routeBase) {
                    // case 'operation':
                    //     routerMap = operationMap;
                    //     break;
                    // case 'exue':
                    //     routerMap = eXueRouterMap;
                    //     break;
                    default: routerMap = homeRouterMap;
                }
                data.data.menus.push({ path: '/' });
                markLvl(data.data.menus, 4)
                // edit by cuihd at 20180815
                filterRoute(routerMap, data.data.menus);
                fixHomePath(routerMap);
                fixRedirect(routerMap);
                routerMap.push({ path: '*', redirect: '/404', hidden: true });
                // console.log(routerMap);
                // console.log(JSON.stringify(routerMap, null, 4));
                // commit('SET_SYSTEM', data.subsys);
                commit('SET_ROUTERS', routerMap);
                resolve();
            })
        }
        // GenerateRoutes({ commit }, data) {
        //     /* global Promise */
        //     return new Promise(resolve => {
        //         const { roles } = data;
        //         const routeBase = location.pathname.substr(1);
        //         let routerMap;
        //         switch (routeBase) {
        //             // case 'operation':
        //             //     routerMap = operationMap;
        //             //     break;
        //             // case 'exue':
        //             //     routerMap = eXueRouterMap;
        //             //     break;
        //             default: routerMap = homeRouterMap;
        //         }
        //         // console.log('routerMap', routerMap);
        //         const accessedRouters = routerMap.filter(v => {
        //             if (roles.indexOf('admin') >= 0) return true;
        //             if (hasPermission(roles, v)) {
        //                 if (v.children && v.children.length > 0) {
        //                     v.children = v.children.filter(child => {
        //                         if (hasPermission(roles, child)) {
        //                             return child
        //                         }
        //                         return false;
        //                     });
        //                     return v
        //                 } else {
        //                     return v
        //                 }
        //             }
        //             return false;
        //         });
        //         commit('SET_ROUTERS', accessedRouters);
        //         resolve();
        //     })
        // }
    }
};


export default permission;